package com.shopping.control;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.dao.impl.BizDaoImpl;
import com.shopping.entity.TbMember;
import com.shopping.service.impl.BizServiceImpl;

/**
 * Servlet implementation class fg_memberServlet
 */
public class fg_memberServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public fg_memberServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException,
            IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");

        BizDaoImpl bdi = new BizDaoImpl();
        BizServiceImpl bsi = new BizServiceImpl();
        bsi.setBizdao(bdi);// 注入

        HttpSession session = request.getSession();

        String name = request.getParameter("name");
        String passwordone = request.getParameter("passwordone");
        String passwordtwo = request.getParameter("passwordtwo");
        String reallyName = request.getParameter("reallyName");
        String age = request.getParameter("age");
        String profession = request.getParameter("profession");
        String email = request.getParameter("email");
        String question = request.getParameter("question");
        String result = request.getParameter("result");

        PrintWriter out = response.getWriter();

        if (name.equals("") || passwordone.equals("") || passwordtwo.equals("") || reallyName.equals("")
                || age.equals(null) || profession.equals("") || email.equals("") || question.equals("")
                || result.equals("")) {// 判断表格是否有空值
            out.print("<script>");
            out.print("alert('表单信息不能为空!');");
            out.print("window.location.href='fg-memberRegister.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            return;
        }
        if (passwordone.equals(passwordtwo)) {// 判断密码是否相同

        } else {
            out.print("<script>");
            out.print("alert('两次密码不一致!');");
            out.print("window.location.href='fg-memberRegister.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
            return;
        }

        // 提交用户信息
        int age1 = Integer.parseInt(age); // 转年龄int类型

        TbMember tbmember = new TbMember();
        tbmember.setName(name);
        tbmember.setPassword(passwordone);
        tbmember.setReallyName(reallyName);
        tbmember.setAge(age1);
        tbmember.setProfession(profession);
        tbmember.setEmail(email);
        tbmember.setQuestion(question);
        tbmember.setResult(result);

        boolean falg1 = bsi.getonetbmember(tbmember);
        if (!falg1) {
            boolean flag = bsi.addtbmember(tbmember);

            session.removeAttribute("add");// 移除初始注册成功隐藏的键值
            request.setAttribute("add", 1);
            request.getRequestDispatcher("fg-four.jsp").forward(request, response);

        } else {
            out.print("<script>");
            out.print("alert('用户名已存在!');");
            out.print("window.location.href='fg-memberRegister.jsp';");
            out.print("</script>");
            out.flush();
            out.close();
        }

    }
}
